﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BIZ;

namespace SinhNhat
{
    public partial class Product : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //hdID.Value = "4F668A85-FC83-4A8E-AC48-A64CBC35C983";

                ProductBIZ productBIZ = new ProductBIZ();
                PRODUCT product = productBIZ.GetByIDForUser(WebUtils.ToGuid(hdID.Value));
                if (product == null)
                {
                    Response.Redirect("/Index.aspx");
                }

                headerTitle.InnerText = product.Name;
                lblName.Text = product.Name;
                txtPrice.Text = product.Price.Value.ToString("N0");
                lrDescription.Text = product.Description;
                imgProduct.ImageUrl = product.Image;
                lblCreatedate.Text = product.CreateDate.Value.ToShortDateString();

                if (product.InStock.Value)
                {
                    lblInstock.Text = "Còn hàng";
                    lblInstock.CssClass = "conhang";
                }
                else
                {
                    lblInstock.Text = "Hết hàng";
                    lblInstock.CssClass = "hethang";
                    btnAddToCard.OnClientClick = "return false;";
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            btnAddToCard.Click += new EventHandler(btnAddToCard_Click);
        }

        void btnAddToCard_Click(object sender, EventArgs e)
        {
            string value = string.Empty;
            
            Dictionary<string, CartObject> listCart = null;
            if (Session[SinhNhat.Properties.Resources.CookieName] == null)
            {
                listCart = new Dictionary<string,CartObject>();
            }
            else
            {
                listCart = (Dictionary<string, CartObject>)Session[SinhNhat.Properties.Resources.CookieName];
            }
            
            CartObject cartObject = null;
            if (listCart.ContainsKey(hdID.Value))
            {
                listCart[hdID.Value].Quantity += 1;
            }
            else
            {
                cartObject = new CartObject(new PRODUCT() { ProductID = WebUtils.ToGuid(hdID.Value) }, 1);
                listCart.Add(hdID.Value, cartObject); 
            }

            Session[SinhNhat.Properties.Resources.CookieName] = listCart;

            Response.Redirect("/Cart.aspx");
        }
    }
}